package com.emip.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emip.bean.ItemBean;
import com.emip.bean.MonitorTypeBean;
import com.emip.dao.ItemDAO;
import com.emip.service.ItemService;

@Service("itemService")
public class ItemServiceImpl implements ItemService {

	@Autowired
	ItemDAO itemDAO;
	
	public ItemBean addItem(String id, MonitorTypeBean monitorType, 
			String name, String abrName, String nationalName, double eMaxValue,
			double eMinValue, double overMaxValue, double overMinValue,
			String standUnit, int scale, double maxRange, double minRange) {
		ItemBean newBean = new ItemBean();
		newBean.setId(id);
		newBean.setMonitorType(monitorType);
		newBean.setName(name);
		newBean.setAbrName(abrName);
		newBean.setNationalName(nationalName);
		newBean.seteMaxValue(eMaxValue);
		newBean.seteMinValue(eMinValue);
		newBean.setOverMaxValue(overMaxValue);
		newBean.setOverMinValue(overMinValue);
		newBean.setStandUnit(standUnit);
		newBean.setScale(scale);
		newBean.setMaxRange(maxRange);
		newBean.setMinRange(minRange);
		//查重，是否已经有相同的monitorType, name的记录存在
		//若有重复记录，则返回null
		if (itemDAO.findExist(newBean)) {
			return null;
		}
		itemDAO.add(newBean);
		return newBean;
	}

	public Boolean deleteItem(String id) {
		ItemBean delBean = itemDAO.findByID(id);
		itemDAO.remove(delBean);
		return true;
	}

	public List<ItemBean> getAll() {
		return itemDAO.getAll();
	}

	public ItemBean getItemByID(String id) {
		return itemDAO.findByID(id);
	}

	public ItemBean updateItem(String id, MonitorTypeBean monitorType, 
			String name, String abrName, String nationalName, double eMaxValue,
			double eMinValue, double overMaxValue, double overMinValue,
			String standUnit, int scale, double maxRange, double minRange) {
		ItemBean newBean = itemDAO.findByID(id);
		newBean.setMonitorType(monitorType);
		newBean.setName(name);
		newBean.setAbrName(abrName);
		newBean.setNationalName(nationalName);
		newBean.seteMaxValue(eMaxValue);
		newBean.seteMinValue(eMinValue);
		newBean.setOverMaxValue(overMaxValue);
		newBean.setOverMinValue(overMinValue);
		newBean.setStandUnit(standUnit);
		newBean.setScale(scale);
		newBean.setMaxRange(maxRange);
		newBean.setMinRange(minRange);
		itemDAO.update(newBean);
		return newBean;
	}

	public List<ItemBean> getItemsByMonitorType(MonitorTypeBean mTypeBean) {
		return itemDAO.getItemsByMonitorType(mTypeBean);
	}

	public ItemBean getItemByName(String name) {
		return itemDAO.findByName(name);
	}

}
